<?php
  global $_GPC, $_W;
        $uniacid = $this->_uniacid;
        $newhouse_id = $_GPC['newhouse_id'];
        $keyword = $_GPC['keyword'];
        $uid = $_W['member']['uid'];
        $tags = 0;
        if ($_GPC['tags']) {
            $tags = $_GPC['tags'];
        }
        /**
         * 获取分类
         */
        if(isset($_GPC['enews']) && ($_GPC['enews'] =='category')){
                                          /* 分类统计 */
            $ret  = pdo_fetchall("select * from ".tablename("site_category")." where uniacid='$uniacid' and parentid=0  and enabled=1");
            $category = array();    
            foreach ($ret as $k => $cat) {
                $key = $cat['id'];
                $category[$key]['name'] = $cat['name'];
                $category[$key]['tags'] = $key;
                $category[$key]['selected'] = ($key == $tags ? 1 : 0);
                $category[$key]['total'] = 0;
            }
            $this->result(0, 'success', $category);
        }
        /**
         * 获取论坛配置参数
         */
        if(isset($_GPC['enews']) && ($_GPC['enews'] =='setting'))
        {
            $ret  = pdo_fetch("select * from ".tablename("kbwdl_thread_forums")." where uniacid='$uniacid'  and type=0 and parentid='0'");
            $setting = iunserializer($ret['setting']) ;
            $ret['setting'] = $setting;
            $ret['thumb_url'] = tomedia($ret['thumb']);
            $this->result(0, 'success', $ret);
        }
   
        /*
         * 获取一片文章
         */
        if(isset($_GPC['enews']) && ($_GPC['enews'] =='show')){
            $id = intval($_GPC['aid']);            
            /* 分类统计 */
             /*更新浏览*/
            pdo_update('kbwdl_thread', array('click +='=>1), array('id'=> $id));
            
            $ret  = pdo_fetch("select * from ".tablename("kbwdl_thread")." where uniacid='$uniacid'  and disabled=0 and id='$id'");
            $ret['smalltext'] =  (htmlspecialchars_decode($ret['smalltext']));
            if(empty($ret['avatar'])){
                   $ret['avatar'] = '/kbwdl_shequzufang/static/images/get_avatar.png';
               }
            $thumbs = unserialize($ret['thumbs']);
                $images = array();
                if(is_array($thumbs)){
                    foreach($thumbs as $k=>$thumb){
                        $images[] = tomedia($thumb);
                    }
                }
                
               $ret['thumbs'] = $images;
               //视频
               if( $ret['video']){
                    $ret['video'] = tomedia( $ret['video']);
               }
              //视频
               if( $ret['thumb']){
                    $ret['thumb'] = tomedia( $ret['thumb']);
               }
               $ret['addtime'] = date('Y-m-d', $ret['addtime']);
           
            $this->result(0, 'success', $ret);
        }
        /**
         * 关注一个发帖人
         */
         if(isset($_GPC['enews']) && ($_GPC['enews'] =='holder')){
             
             $data['blog_uid'] = intval($_GPC['blog_uid']);
             $data['blog_name'] = trim($_GPC['blog_name']);
             $data['uid'] = $uid;
             $data['uniacid'] = $uniacid;
             $data['fid'] = intval($_GPC['fid']);
             $data['fname'] = trim($_GPC['fname']);
             if($_GPC['htype']==1){ /*关注粉丝*/
                $has = pdo_fetch("select id from ".tablename("kbwdl_thread_fans"). " where uid='$uid' and blog_uid='{$data['blog_uid']}'"); 
             }
             if($_GPC['htype']==2){ /*关注版块*/
                $has = pdo_fetch("select id from ".tablename("kbwdl_thread_fans"). " where uid='$uid' and fid='{$data['fid']}'"); 
             }
             
             if(empty($has)){
                 pdo_insert("kbwdl_thread_fans", $data);
                 
                 $this->result(0, 'success', $data);
             }else{
                 $this->result(0, '已经关注过',  $data);
             }
         }
        /**
         * 默认获取整个列表
         */
        $limit = 10;
        if (isset($_GPC['limit']) && !empty($_GPC['limit'])) {
            $limit = intval($_GPC['limit']);
        }
        $sql_add = " s.uniacid='$uniacid' ";
 
        if ($tags) {
           // $sql_add.=" and s.fid in (select id from ".tablename("site_category")." where (id='$tags' or parentid='$tags')) ";
        }
        if ($keyword) {
            $sql_add.=" and ( s.title like '%$keyword%' )";
        }
        $page = max(1, $_GPC['page']);
        $pagesize = $limit;
        $startlimit = ($page - 1) * $pagesize;
        $category = array();
        if(isset($_GPC['total']) && ! empty($_GPC['total'])){
            $total = $_GPC['total'];   
       
        }else{
             $total = pdo_fetchcolumn("select count(*)  from ". tablename("kbwdl_thread") ." s where $sql_add ");

        }
       
        $ret = pdo_fetchall("select *  from " . tablename("kbwdl_thread") .
                " s   where $sql_add order by s.addtime desc limit $startlimit,$pagesize ");
        if (!empty($ret)) {
            foreach ($ret as $key => $val) {
              // $ret[$key]['category'] = $this->other['saletype'][$val['tags']];
                $ret[$key]['showtime'] = date('Y年m月d日', $val['addtime']);
                if(empty($ret[$key]['avatar'])){
                    $ret[$key]['avatar'] = '/kbwdl_shequzufang/static/images/get_avatar.png';
                }
                $thumbs = unserialize($val['thumbs']);
                $images = array();
                if(is_array($thumbs)){
                    foreach($thumbs as $k=>$thumb){
                        $images[] = tomedia($thumb);
                    }
                }
               $ret[$key]['thumbs'] = $images;
            }
        }
        $data['salelist'] = $ret;

        $data['category'] = $category;
        $data['total'] = $total;
        $this->result(0, 'success', $data);
?>
